/**
* Oxide
*
* Copyright (c) 2011 Dinosaur Kick Pty Ltd, All rights reserved.
*
* This source code is made available under the "Modified BSD" licensing arrangement.
* See license.txt for details.
*/
// ----------------------------------------------------------------------------

bool dispatch_numeric(void)
{
	if (*m_cursor != '0')
	{
		return lex_numeric_dec();
	}

	m_cursor.next();
	char_type ch = *m_cursor;

	if (ch == 'x')
	{
		return lex_integer_hex();
	}
	else if (ch == 'b')
	{
		return lex_integer_bin();
	}
	else if (std::isdigit(ch))
	{
		return lex_integer_oct();
	}

	setup_token(TOK_INTEGER_DEC, '0');
	return true;
}

// ----------------------------------------------------------------------------

bool lex_integer_hex(void)
{
	assert(*m_cursor == 'x');
	setup_token(TOK_INTEGER_HEX, O2_CSTR("0x"));
	m_cursor.next();
	char_type ch;

	while (m_cursor && std::isdigit(ch = *m_cursor) || std::isxdigit(ch))
	{
		m_token.content += m_cursor.get();
	}
	
	return m_token.content.size() > 2;
}

// ----------------------------------------------------------------------------

bool lex_integer_bin(void)
{
	assert(*m_cursor == 'b');
	setup_token(TOK_INTEGER_BIN, O2_CSTR("0b"));
	m_cursor.next();
	char_type ch = 0;

	while (m_cursor && m_cursor.advance_if('0', '1', ch))
	{
		m_token.content += ch;
	}

	return ch != 0;
}

// ----------------------------------------------------------------------------

bool lex_integer_oct(void)
{
	setup_token(TOK_INTEGER_OCT, '0');

	char_type ch = 0;

	while (m_cursor && m_cursor.advance_if('0', '8', ch))
	{
		m_token.content += ch;
	}

	return ch != 0;
}

// ----------------------------------------------------------------------------

bool lex_numeric_dec(void)
{
	assert(std::isdigit(*m_cursor));
	setup_token(TOK_INTEGER_DEC);
	char_type ch = 0;

	while (m_cursor)
	{
		ch = *m_cursor;

		if (std::isdigit(ch))
		{
			m_token.content += m_cursor.get();
			continue;
		}
		else if (ch == TOK_DOT && m_token.identity == TOK_INTEGER_DEC)
		{
			m_token.identity = TOK_FIXED;
			m_token.content += m_cursor.get();
			continue;
		}
		else if (ch == 'f')
		{
			m_token.identity = TOK_FLOAT;
			m_token.content += m_cursor.get();
		}
		break;
	}
	
	return true;
}

// ----------------------------------------------------------------------------
